package net.boot.smpp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author qiusu
 * @since  2025/08/18
 */
@RestController
@RequestMapping("/smpp")
public class SmppStatusController {

    @Autowired
    private ThreadPoolTaskExecutor waitBindExecService;

    @Autowired
    private ThreadPoolTaskScheduler deliveryExecService;

    @GetMapping("/thread-status")
    public Map<String, Object> getThreadStatus() {
        Map<String, Object> status = new HashMap<>();

        // 检查 waitBindExecService 线程池状态
        if (waitBindExecService != null) {
            Map<String, Object> waitBindStatus = new HashMap<>();
            ThreadPoolExecutor executor = waitBindExecService.getThreadPoolExecutor();
            waitBindStatus.put("activeCount", executor.getActiveCount());
            waitBindStatus.put("poolSize", executor.getPoolSize());
            waitBindStatus.put("corePoolSize", waitBindExecService.getCorePoolSize());
            waitBindStatus.put("maximumPoolSize", waitBindExecService.getMaxPoolSize());
            waitBindStatus.put("queueSize", executor.getQueue().size());
            waitBindStatus.put("isRunning", !executor.isTerminated());
            status.put("waitBindExecService", waitBindStatus);
        }

        // 检查 deliveryExecService 线程池状态
        if (deliveryExecService != null) {
            Map<String, Object> deliveryStatus = new HashMap<>();
            // ThreadPoolTaskScheduler 不提供直接访问 ThreadPoolExecutor 的方法
            // 我们只能获取一些基本信息
            deliveryStatus.put("poolSize", deliveryExecService.getPoolSize());
            deliveryStatus.put("isRunning", true); // ThreadPoolTaskScheduler 通常一直运行
            deliveryStatus.put("activeCount", "N/A"); // 无法直接获取
            deliveryStatus.put("queueSize", "N/A"); // 无法直接获取
            status.put("deliveryExecService", deliveryStatus);
        }

        return status;
    }

    @GetMapping("/health")
    public Map<String, Object> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", System.currentTimeMillis());

        Map<String, Object> threadPools = new HashMap<>();

        if (waitBindExecService != null) {
            ThreadPoolExecutor executor = waitBindExecService.getThreadPoolExecutor();
            threadPools.put("waitBindExecService", !executor.isTerminated() ? "RUNNING" : "STOPPED");
        }

        if (deliveryExecService != null) {
            // ThreadPoolTaskScheduler 通常被认为是运行的，除非整个应用关闭
            threadPools.put("deliveryExecService", "RUNNING");
        }

        health.put("threadPools", threadPools);
        return health;
    }

    @PostMapping("/sendTest")
    public Map<String, Object> sendTest(@RequestBody Map<String, Object> params) {
        Map<String, Object> status = new HashMap<>();

        return status;
    }

}
